package com.wenchao.quickstart.utils;

import android.text.TextUtils;

import java.util.regex.Pattern;

/**
 * @author zhangwenchao
 */
public class PayBarCodeUtil {

    public enum PayBarCodeType {
        WEI_XIN_PAY,
        ALI_PAY,
        MEMBER_PAY,
        OTHER
    }

    public static PayBarCodeType getPayBarCodeType(String barcode) {
        if (IsWxBarCode(barcode)) {
            return PayBarCodeType.WEI_XIN_PAY;
        } else if (IsZfbBarCode(barcode)) {
            return PayBarCodeType.ALI_PAY;
        } else if (IsMemberBarCode(barcode)) {
            return PayBarCodeType.MEMBER_PAY;
        } else {
            return PayBarCodeType.OTHER;
        }
    }

    /// <summary>
    /// 验证微信条码付格式
    /// </summary>
    /// <param name="barCode">条码</param>
    /// <returns>True:验证合格</returns>
    private static boolean IsWxBarCode(String barCode) {
        boolean result = false;
        if (!TextUtils.isEmpty(barCode)) {
            if (barCode.length() == 18) {
                try {
                    int startNum = Integer.parseInt(barCode.substring(0, 2));
                    if (startNum >= 10 && startNum <= 15) {
                        result = true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return result;
    }

    /// <summary>
    /// 验证支付宝条码付格式
    /// </summary>
    /// <param name="barCode">条码</param>
    /// <returns>True:验证合格</returns>
    private static boolean IsZfbBarCode(String barCode) {
        boolean result = false;
        if (!TextUtils.isEmpty(barCode)) {
            if (barCode.length() >= 16 && barCode.length() <= 24) {
                try {
                    int startNum = Integer.parseInt(barCode.substring(0, 2));
                    if (startNum >= 25 && startNum <= 30) {
                        result = true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return result;
    }

    /// <summary>
    /// 验证会员条码付格式
    /// </summary>
    /// <param name="barCode">条码</param>
    /// <returns>True:验证合格</returns>
    private static boolean IsMemberBarCode(String authCode) {
        boolean result = false;
        if (!TextUtils.isEmpty(authCode)) {
            if (Pattern.matches("^HJ50.*", authCode)) {
                result = true;
            } else {
                result = false;
            }
        } else {
            result = false;
        }
        return result;
    }

}
